﻿using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using System.Drawing;
using FFmpeg.AutoGen;
using Microsoft.UI.Xaml.Media.Imaging;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Graphics.Canvas;
using FFmpegWinui3.Views;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace FFmpegWinui3
{
    /// <summary>
    /// An empty window that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainWindow : Window
    {
        string url = @"C:\Users\ludin\Desktop\新建文件夹 (4)\1.mp4";

        CanvasBitmap bitmap;
        public MainWindow()
        {
            this.InitializeComponent();

            myframe.Navigate(typeof(MediaPage));
        }

       

        ///// <summary>
        ///// 编码
        ///// </summary>
        //private static unsafe void EncodeImagesToH264()
        //{
        //    //获取所有图片
        //    var frameFiles = Directory.GetFiles(".", "frame.*.jpg").OrderBy(x => x).ToArray();
        //    //获取第一张图片
        //    var fistFrameImage = Image.FromFile(frameFiles.First());
        //    //输出名
        //    var outputFileName = "out.mp4";
        //    //帧数
        //    var fps = 60;
        //    //大小
        //    var sourceSize = fistFrameImage.Size;
        //    //格式
        //    var sourcePixelFormat = AVPixelFormat.AV_PIX_FMT_BGR24;
        //    //目标大小
        //    var destinationSize = sourceSize;
        //    //目标图像格式
        //    var destinationPixelFormat = AVPixelFormat.AV_PIX_FMT_YUV420P;
        //    //视频帧转换对象
        //    using var vfc =
        //        new VideoFrameConverter(sourceSize, sourcePixelFormat, destinationSize, destinationPixelFormat);
        //    //打开文件流
        //    using var fs = File.Open(outputFileName, FileMode.Create);
        //    //将数据写入文件流
        //    using var vse = new H264VideoStreamEncoder(fs, fps, destinationSize);

        //    var frameNumber = 0;
        //    //遍历所有图片
        //    foreach (var frameFile in frameFiles)
        //    {
        //        //字节数组
        //        byte[] bitmapData;
        //        //打开图片
        //        using (var frameImage = Image.FromFile(frameFile))
        //        using (var frameBitmap = frameImage is Bitmap bitmap ? bitmap : new Bitmap(frameImage))
        //            bitmapData = GetBitmapData(frameBitmap);

        //        fixed (byte* pBitmapData = bitmapData)
        //        {
        //            var data = new byte_ptrArray8 { [0] = pBitmapData };
        //            var linesize = new int_array8 { [0] = bitmapData.Length / sourceSize.Height };
        //            var frame = new AVFrame
        //            {
        //                data = data,
        //                linesize = linesize,
        //                height = sourceSize.Height
        //            };
        //            var convertedFrame = vfc.Convert(frame);
        //            convertedFrame.pts = frameNumber * fps;
        //            vse.Encode(convertedFrame);
        //        }

        //        Console.WriteLine($"frame: {frameNumber}");
        //        frameNumber++;
        //    }
        //}

        ////获取图片数据
        //private static byte[] GetBitmapData(Bitmap frameBitmap)
        //{
        //    var bitmapData = frameBitmap.LockBits(new Rectangle(Point.Empty, frameBitmap.Size),
        //        ImageLockMode.ReadOnly,
        //        PixelFormat.Format24bppRgb);

        //    try
        //    {
        //        var length = bitmapData.Stride * bitmapData.Height;
        //        var data = new byte[length];
        //        Marshal.Copy(bitmapData.Scan0, data, 0, length);
        //        return data;
        //    }
        //    finally
        //    {
        //        frameBitmap.UnlockBits(bitmapData);
        //    }
        //}

    }
}
